﻿using UnityEngine;
using System.Collections;

public class ObjectPoolManager : Singleton<ObjectPoolManager>
{
    ObjectPool<Rigidbody2D> stonePool;
    ObjectPool<BirdCommon> commonBirdPool;
    [SerializeField]
    GameObject m_StonePrefab;
    [SerializeField]
    GameObject m_CommonBirdPrefab;
    protected override void Awake()
    {
        base.Awake();
        stonePool = new ObjectPool<Rigidbody2D>(() => { return Instantiate(m_StonePrefab).GetComponent<Rigidbody2D>(); }, 32);
        commonBirdPool = new ObjectPool<BirdCommon>(() => { return Instantiate(m_CommonBirdPrefab).GetComponent<BirdCommon>(); }, 32);
    }
    public Rigidbody2D GetStone()
    {
        Rigidbody2D stone = stonePool.GetObject();
        stone.gameObject.SetActive(true);
        return stone;
    }
    public BirdCommon GetBird()
    {
        BirdCommon bird = commonBirdPool.GetObject();
        bird.gameObject.SetActive(true);
        return bird;
    }
    IEnumerator RecircleStoneCor(Rigidbody2D stone)
    {
        yield return new WaitForSeconds(8f);
        stone.gameObject.SetActive(false);
        stonePool.RecircleObject(stone);
    }
    public void RecircleStone(Rigidbody2D stone)
    {
        StartCoroutine(RecircleStoneCor(stone));
    }
    IEnumerator RecircleCommonBirdCor(BirdCommon bird)
    {
        yield return new WaitForSeconds(8f);
        bird.gameObject.SetActive(false);
        commonBirdPool.RecircleObject(bird);
    }
    public void RecircleCommonBird(BirdCommon bird)
    {
        StartCoroutine(RecircleCommonBirdCor(bird));
    }
}
